
package com.qiji.video.web.interceptor;


import com.qiji.video.exception.ServiceException;
import com.qiji.video.util.JwtUtil;
import lombok.SneakyThrows;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.subject.Subject;
import org.apache.shiro.web.filter.authc.BasicHttpAuthenticationFilter;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class JwtInterceptor extends BasicHttpAuthenticationFilter {
    // 跨域支持
    @Override
    protected boolean preHandle(ServletRequest request, ServletResponse response) throws Exception {
        HttpServletRequest httpServletRequest = (HttpServletRequest) request;
        HttpServletResponse httpServletResponse = (HttpServletResponse) response;
        httpServletResponse.setHeader("Access-control-Allow-Origin", httpServletRequest.getHeader("Origin"));
        httpServletResponse.setHeader("Access-Control-Allow-Methods", "GET,POST,OPTIONS,PUT,DELETE");
        httpServletResponse.setHeader("Access-Control-Allow-Headers", httpServletRequest.getHeader("Access-Control-Request-Headers"));
        // 跨域时会首先发送一个 option请求，这里我们给 option请求直接返回正常状态
        if (httpServletRequest.getMethod().equals(RequestMethod.OPTIONS.name())) {
            httpServletResponse.setStatus(HttpStatus.OK.value());
            return false;
        }
        return super.preHandle(request, response);
    }

    // 判断是否登录，返回false执行onAccessDenied方法，true通过
    @SneakyThrows
    @Override
    protected boolean isAccessAllowed(ServletRequest request, ServletResponse response, Object mappedValue) {
        System.out.println("isAccessAllowed");
        HttpServletRequest httpServletRequest = (HttpServletRequest) request;
        HttpServletResponse httpServletResponse = (HttpServletResponse) response;
        httpServletRequest.setCharacterEncoding("UTF-8");
        httpServletResponse.setCharacterEncoding("UTF-8");
        String token = httpServletRequest.getHeader("token");
        if (token == null){
            throw new ServiceException("未登录、请先登录");
        }
        try{
            JwtUtil.checkSign(token);
        }catch (Exception e){
            e.printStackTrace(); //打印token验证失败的原因
            throw new ServletException("token无效，请重新登录");
        }
        return true;
    }

    @Override
    protected boolean executeLogin(ServletRequest request, ServletResponse response) throws Exception {
        return super.executeLogin(request, response);
    }
    //    @Override
//    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception{
//        // 1.如果接收到请求，预检请求，浏览器发送
//        String method = request.getMethod();
//        if(method.equals("OPTIONS")){
//            // 不拦截预检请求
//            return true;
//        }
//        String token = request.getHeader("token");
//        if(token == null){
////            return true;
//            throw new ServletException("未登录、请先登录");
//        }
//        try{
//            JwtUtil.checkSign(token);
//        }catch (Exception e){
//            e.printStackTrace(); //打印token验证失败的原因
//            throw new ServletException("token无效，请重新登录");
//        }
//        // 通过上述验证，通过拦截
//        return true;
//    }
}
